<?php
namespace Admin\Controller;
use Org\Util\Page;

class UsedController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('used');
        $this->_cate_mod = D('AreaCate');
        $this->status = getStatus('used');
    }
    public function index (){
        $map = $this->_search();
        $mod = D($this->_name);
        !empty($mod) && $this->_list($mod, $map);
        $this->options = $this->check();
        $this->display();
    }
    protected function _list($model, $map = array(), $pagesize=20){
        //角色信息
        $info = D ('Admin')->get_role ($_SESSION['id']);
        if (empty ($info['scope'])) $this->error('请去完善个人信息','admin.php/index/index',5);
        $users = classify ($info['level'], $info['scope']);
        $map['u.user_id'] = array ('in', $users);
        $map[] = 'u.dict_id=d.id';
        $field = 'u.*,d.build,d.fool,d.room';
        $table = array ('yk_used'=>'u','yk_dict'=>'d');
        $count = $model->field($field)->table($table)->where($map)->count('u.id');
        $pager = new Page($count, $pagesize);
        $page = $pager->show();
        $this->assign("page", $page);

        $list = $model->get_houses ($map, $pager->firstRow, $pager->listRows);
        $this->assign('list', $list);
        $this->assign('list_table', true);
    }
    public function _before_index() {
        $p = I('p',1,'intval');
        $this->list_relation = true;
        $this->assign('p',$p);
    }

    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $area_id = I('get.area_id',0,'int');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($area_id) {
            $id_arr = $this->_cate_mod->get_child_ids($area_id, true);
            $map['area_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$area_id))->getField('spid');
            $selected_ids = $spid ? $spid . $area_id : $area_id;
        }

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'area_id' => $area_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }

    public function _before_add(){
        //获取小区信息
        $this->one_estate = M('estate')->where(array('id'=>intval($_GET['eid'])))->field('id,title')->find();
        $this->estate = M('estate')->where(array('status'=>1))->field('id,title')->select();
        $this->menu = getLinkMenuFor(array(24),1);
    }

    protected function _before_insert($data) {
        $data['add_time']   = time();
        $data['used_type'] = !empty($data['used_type']) ? '|'.implode('|',$data['used_type']).'|' : '';
        $data['mating'] = !empty($data['mating']) ? '|'.implode('|',$data['mating']).'|' : '';
        return $this->_insertPic($data);
    }

    public function _before_edit(){
        $id = I('get.id');
        $info = $this->_mod->field('id,file,mating,used_type,area_id,user_id')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$info['area_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $info['area_id'];
        }else{
            $spid .= $info['area_id'];
        }
        $this->estate = M('estate')->where(array('status'=>1))->field('id,title')->select();
        $this->menu = getLinkMenuFor(array(24),1);
        $this->assign('img_list',json_decode($info['file'],true));
        $this->assign('used_type',explode('|',trim($info['used_type'],'|')));
        $this->assign('mating',explode('|',trim($info['mating'],'|')));
        $this->assign('selected_ids',$spid);
        if($info['user_id']){
            $this->user = M('user')->where(array('id'=>$info['user_id']))->find();
        }
    }

    protected function _before_update($data) {
        $data['used_type'] = !empty($data['used_type']) ? '|'.implode('|',$data['used_type']).'|' : '';
        $data['mating'] = !empty($data['mating']) ? '|'.implode('|',$data['mating']).'|' : '';
        $new_data = $this->_updatePic($data);
        $info = M('used')->where(array('id'=>$new_data['id']))->find();
        if($info['is_index'] == 1 && $new_data['img'] && $_FILES['img']['name']){
            $info['img'] = $new_data['img'];
            D('Position')-> where(array('hid'=>$info['id'],'model'=>'used'))->save(array('data'=>serialize($info)));
        }
        return $new_data;
    }
    //审核
    public function examine (){

    }

    protected function _after_insert($id){

    }

    protected function _after_update($id){

    }

    public function ajaxCheckTitle(){
        $title = urldecode(I('get.J_title',''));
        if($title){
            $info = $this->_mod->where(array('title'=>$title))->find();
            if($info){
                $this->ajaxReturn(0);
            }else{
                $this->ajaxReturn(1);
            }
        }else{
            $this->ajaxReturn(1);
        }
    }


}